﻿Public Class frmUserManager
    Private userDa As New DSVaultingTableAdapters.user_acountTableAdapter
    Private action As String
    Private actType As String() = {"NONE", "INSERT", "UPDATE", "DELETE"}
    Private slcID As Long

    Private Sub frmUserManager_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        uxUserRolCbx.SelectedIndex = 0
        uxUSDNoteDgv.DataSource = userDa.GetData
    End Sub

    Private Sub uxInputNoteSavebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxInputNoteSavebtn.Click
        Dim username As String = uxUserNameTbx.Text.Trim.ToLower
        Dim userpass As String = uxUserPassTbx.Text
        Dim userrole As String = uxUserRolCbx.SelectedItem
        If Not username = "" And Not userpass = "" Then
            If action = actType(1) Then
                userDa.Insert(username, userpass, userrole)
                uxUserNameTbx.Clear()
                uxUserPassTbx.Clear()
                updateDGV()
            ElseIf action = actType(2) Then
                If MessageBox.Show("តើអ្នកពិតជាចងរក្សាទុកព័ត៌មានដែលបានកែប្រែមែនទេ?", "សារបញ្ជាក់ពីការកែប្រែ", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
                    userDa.Update(username, userpass, userrole, slcID)
                    updateDGV()
                End If
            End If
        End If
    End Sub

    Private Sub uxInputNewNotbtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxInputNewNotbtn.Click
        uxUserNameTbx.Enabled = True
        uxUserPassTbx.Enabled = True
        action = actType(1)
    End Sub

    Private Sub uxEditNotebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxEditNotebtn.Click
        If uxUSDNoteDgv.SelectedRows.Count > 0 Then
            uxUserNameTbx.Enabled = True
            uxUserPassTbx.Enabled = True
            uxUserNameTbx.Text = uxUSDNoteDgv.SelectedRows.Item(0).Cells("username").Value
            uxUserPassTbx.Text = uxUSDNoteDgv.SelectedRows.Item(0).Cells("password").Value
            uxUserRolCbx.Text = uxUSDNoteDgv.SelectedRows.Item(0).Cells("roles").Value
            If Not uxUserNameTbx.Text = "" And Not uxUserPassTbx.Text = "" Then
                slcID = Long.Parse(uxUSDNoteDgv.SelectedRows.Item(0).Cells("Index").Value)
                action = actType(2)
            End If
        End If
    End Sub

    Private Sub uxDeleteNotebtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxDeleteNotebtn.Click
        If MessageBox.Show("តើអ្នកពិតជាចង់លុបចោលទិន្នន័យនេះមែនទេ?", "សារបញ្ជាក់ពីការលុបចោល", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Yes Then
            Dim oldId = Long.Parse(uxUSDNoteDgv.SelectedRows.Item(0).Cells("Index").Value)
            userDa.Delete(oldId)
            action = actType(3)
            Me.updateDGV()
        End If
    End Sub

    Private Sub updateDGV()
        Dim slcItem As Integer = uxUSDNoteDgv.SelectedRows.Item(0).Index
        uxUSDNoteDgv.DataSource = userDa.GetData
        If Not action = actType(3) Then
            uxUSDNoteDgv.Item("username", slcItem).Selected = True
        End If
    End Sub

    Private Sub uxCancelBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxCancelBtn.Click
        frmSetting.Enabled = True
        Me.Close()
    End Sub

    Private Sub frmUserManager_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        frmSetting.Enabled = True
    End Sub

    Private Sub uxUSDNoteDgv_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles uxUSDNoteDgv.CellDoubleClick
        uxEditNotebtn_Click(sender, e)
    End Sub
End Class